<template>
  <div class="app-container customer-div">
    <div>
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="50px">
        <el-form-item label="日期：">
          <el-date-picker
            v-model="listQuery.date"
            type="daterange"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleSearchList()"
            type="primary">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="myChart" style="height:300px;"></div>
    <div>
      <div style="padding:10px 0;font-size:14px;">目标总量：{{ allTargetNum }}吨，实际销售吨数：{{ allGoodsNum }}吨，总完成率：{{ allCompletionRate }}%</div>
      <el-radio-group v-model="listQuery.status" size="small" @change="handleSearchList" style="margin-top:10px;">
        <el-radio-button label="0">全部</el-radio-button>
        <el-radio-button label="1">待处理</el-radio-button>
      </el-radio-group>
    </div>
    <!--数据列表-->
    <div class="table-container">
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column  label="日期"  align="center" min-width="100">
          <template slot-scope="scope">
            {{scope.row.time}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="品名" min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsClass}}
          </template>
        </el-table-column>
        <el-table-column label="负责人" align="center" min-width="100">
          <template slot-scope="scope">
            {{scope.row.userName}}
          </template>
        </el-table-column>

        <el-table-column min-width="100" label="当日目标量" align="center">
          <template slot-scope="scope">
            {{scope.row.targetNum}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="100"
          label="当天销售吨数">
          <template  slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          min-width="100"
          label="当天完成率">
          <template  slot-scope="scope">
            {{scope.row.completionRate}}%
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="150"
          label="目标差异原因">
          <template  slot-scope="scope">
            {{scope.row.difRemark}}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          min-width="150"
          label="毛利低于20或者毛利超过预期的原因分析">
          <template  slot-scope="scope">
            {{scope.row.profit}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          min-width="150"
          label="改进方法">
          <template  slot-scope="scope">
            {{scope.row.reason}}
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          min-width="150"
          label="更新时间">
          <template  slot-scope="scope">
            {{scope.row.updateTime}}
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20,50,100]"
        :total="count">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {purchaseTargetList,purchaseChart} from '@/api/company'
const defaultListQuery={
  "isMy":1,
  "type" : '2',  //1产品线目标  2采购目标
  "status" : "0", //1 待处理  0 全部
  "date" : [], //日期
  "pageNum" : 1,
  "pageSize" : 10
};

export default {
  name: "myPurchaseTarget",
  data(){
    return {
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      btnLoading:false,
      allTargetNum:'',
      allCompletionRate:'',
      allGoodsNum:''
    }
  },

  computed: {

  },
  created() {
    this.getList();
    this.getCharts()
  },
  methods:{
    getCharts(){
      purchaseChart(this.listQuery).then((res)=>{
        this.draw1(res.x,res.y1.res.y2)
      })
    },
    //绘制图表
    draw1(x,y1,y2){
      let myChart = this.$echarts.init(document.getElementById('myChart'));
      // 绘制图表
      myChart.setOption({
        xAxis: {
          type: 'category',
          data: x
        },
        legend: {
          data: ['销售吨数', '目标吨数']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name:'销售吨数',
            data: y1,
            type: 'bar',
            barWidth : 30,//柱图宽度
          },
          {
            name:'目标吨数',
            data: y2,
            type: 'bar',
            barWidth : 30,//柱图宽度
          }
        ]
      });
    },

    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
      this.getCharts()
    },

    //获取列表
    getList() {
      this.listLoading = true;
       purchaseTargetList(this.listQuery).then(response => {
           this.tableData = response.list;
           this.count = response.count;
           this.listLoading = false;
           this.allCompletionRate = response.allCompletionRate;
           this.allGoodsNum = response.allGoodsNum;
           this.allTargetNum = response.allTargetNum;
        }).catch(()=>{
          this.listLoading = false;
        });
    },
  },
}
</script>

<style scoped lang="scss">

</style>
